# Contributor: larrabee <kafatron@gmail.com>
# Maintainer: larrabee <kafatron@gmail.com>
pkgname=mozjpeg
pkgver=4.0.0
pkgrel=0
pkgdesc="accelerated baseline JPEG compression and decompression library based on libjpeg-turbo"
url="https://github.com/mozilla/mozjpeg"
arch="all"
license="IJG"
depends="libpng"
makedepends="nasm cmake libpng-dev"
replaces="libjpeg libjpeg-turbo"
subpackages="$pkgname-doc $pkgname-dev $pkgname-utils"
source="$pkgname-$pkgver.tar.gz::https://github.com/mozilla/mozjpeg/archive/v$pkgver.tar.gz"

builddir="$srcdir/$pkgname-$pkgver"
options="!check"

build() {
	local _floattest=
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	case "$CARCH" in
		ppc64le) export JSIMD_FORCENONE=1;;
		s390x) _floattest="-DFLOATTEST=fp-contract";;
	esac
	cmake \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib \
		-DBUILD_SHARED_LIBS=True \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DWITH_JPEG8=1 \
		-DENABLE_STATIC=0 \
		$_floattest \
		$CMAKE_CROSSOPTS
	make
}

check() {
	make test
}

package() {
	make -j1 DESTDIR="$pkgdir" install
	install -d "$pkgdir"/usr/share/licenses/$pkgname
	ln -s ../../doc/libjpeg-turbo/LICENSE.md "$pkgdir/usr/share/licenses/$pkgname"
}

utils() {
	pkgdesc="Utilities for manipulating JPEG images"
	replaces="jpeg libjpeg-turbo-utils"
	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/bin "$subpkgdir"/usr/
}

doc() {
	default_doc
	replaces="jpeg-doc libjpeg-turbo-doc"
}

dev() {
	default_dev
	replaces="jpeg-dev libjpeg-turbo-dev"
}

sha512sums="1545c10e9e0f960e73db1a940ee7dc25c5ade3b848b3be74baf28aa11a3d9255f7f4442e769e31c7d71db087c2c187a469a9a59545a28e6b1ba3489a54b3716f  mozjpeg-$pkgver.tar.gz"